//
//  HWHomeNormalFieryGameCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/11/16.
//  Copyright © 2023 94Wan. All rights reserved.
//

import UIKit

class HWHomeNormalFieryGameCell: UICollectionViewCell {
    var bgImageView:UIImageView!
    var logoImageView:UIImageView!
    var tagImageView:UIImageView!
    var starImageView:UIImageView!
    var scoreLabel:UILabel!
    var nameLabel:UILabel!
    var alphaView:UIView!
    var textTagCollectionView:TTGTextTagCollectionView!
    
    var model:HWHomeGameModel? {
        didSet {
            bgImageView.sd_setImage(with: URL(string: model?.image ?? ""), completed: nil)
            logoImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.name
            scoreLabel.text = model?.point
            
//            for (idx,txt) in (model?.biaoqian ?? []).enumerated() {
//                let content = TTGTextTagStringContent.init(text: txt)
//                content.textFont = UIFont.systemFont(ofSize: 8*widthScale)
//                if idx == 0 {
//                    content.textColor = UIColor.colorWithHexString(hexString: "#F87706")
//                }else if idx == 1 {
//                    content.textColor = UIColor.colorWithHexString(hexString: "#EBAE38")
//                }else {
//                    content.textColor = UIColor.colorWithHexString(hexString: "#FF5D5D")
//                }
//                let normalStyle = TTGTextTagStyle.init()
//                normalStyle.backgroundColor = UIColor.colorWithHexString(hexString: "#FFEDDE")
//                normalStyle.cornerRadius = 2*widthScale
//                normalStyle.extraSpace = CGSize.init(width: 2*widthScale, height: 1*widthScale)
//
//                let tag = TTGTextTag.init()
//                tag.content = content
//
//                tag.style = normalStyle
//                textTagCollectionView.addTag(tag)
//            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWHomeNormalFieryGameCell {
    private func initUI() -> Void {
        contentView.backgroundColor = .white
        
        bgImageView = UIImageView()
        bgImageView.backgroundColor = .black
        bgImageView.contentMode = .scaleAspectFill
        bgImageView.layer.masksToBounds = true
        bgImageView.layer.cornerRadius = 7*widthScale
        contentView.addSubview(bgImageView)
        
        alphaView = UIView()
        alphaView.frame = CGRect(x: 0, y: 0, width: 168*widthScale, height: 27*widthScale)
//        alphaView.backgroundColor = .colorWithAlphaHexString(hexString: "#2F2F2F", alpha: 0.2)
//        alphaView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#7B7B7B", alpha: 0.2).cgColor
//        alphaView.layer.shadowOffset = CGSize(width: 0, height: 3)
//        alphaView.layer.shadowRadius = 0
//        alphaView.layer.shadowOpacity = 5
        
        let blur = UIBlurEffect(style: .light)
        let blurView = UIVisualEffectView(effect: blur)
        blurView.frame = CGRect(x: 0, y: 0, width: 168*widthScale, height: 27*widthScale)
        blurView.backgroundColor = .colorWithAlphaHexString(hexString: "#2F2F2F", alpha: 0.16)
        alphaView.addSubview(blurView)
        bgImageView.addSubview(alphaView)
        
        tagImageView = UIImageView()
        tagImageView.image = UIImage(named: "homeHeavyTag")
        bgImageView.addSubview(tagImageView)
        
        logoImageView = UIImageView()
        logoImageView.layer.cornerRadius = 8*widthScale
        logoImageView.layer.masksToBounds = true
        bgImageView.addSubview(logoImageView)
        
        nameLabel = HWLabel.createLabel(title: "", color: "#FFFFFF", font: 10*widthScale)
        nameLabel.numberOfLines = 1
        bgImageView.addSubview(nameLabel)
        
        starImageView = UIImageView()
        starImageView.image = UIImage(named: "homeYellowStar")
        bgImageView.addSubview(starImageView)
        
        scoreLabel = HWLabel.createBoldLabel(title: "", color: "#F2C36E", font: 11*widthScale)
        bgImageView.addSubview(scoreLabel)
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.delegate = self;
        textTagCollectionView.alignment = TTGTagCollectionAlignment.right;
        textTagCollectionView.manualCalculateHeight = true;
        bgImageView.addSubview(textTagCollectionView)
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        alphaView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(27*widthScale)
        }
        
        tagImageView.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalTo(7*widthScale)
            make.width.equalTo(40*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        logoImageView.snp.makeConstraints { make in
            make.left.equalTo(4*widthScale)
            make.bottom.equalTo(-4*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(logoImageView.snp.right).offset(4*widthScale)
            make.centerY.equalTo(logoImageView)
            make.right.equalTo(bgImageView.snp.centerX)
        }
        
        starImageView.snp.makeConstraints { make in
            make.width.height.equalTo(11*widthScale)
            make.right.equalTo(scoreLabel.snp.left).offset(-2*widthScale)
            make.centerY.equalTo(logoImageView)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.centerY.equalTo(logoImageView)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(self.bgImageView.snp.centerX)
            make.right.equalTo(-10*widthScale)
            make.height.equalTo(13*widthScale)
            make.bottom.equalTo(-9*widthScale)

        }
    }
}

extension HWHomeNormalFieryGameCell:TTGTextTagCollectionViewDelegate{
    func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {

    }
}
